<template>
  <section>
    <!--工具条，就是高级查询的那个框-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="店铺名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getQueryShops">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="this.pageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column prop="name" label="店铺名称" width="120">
      </el-table-column>
      <el-table-column prop="manager.realName" label="店长" width="120">
      </el-table-column>
      <el-table-column prop="tel" label="店铺电话" width="120">
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" min-width="180">
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="180">
        <template scope="scope">
          <span v-if="scope.row.state == 1" style="color: #d9a51d;">待审核</span>
          <span v-if="scope.row.state == 2" style="color:#1985d3">审核通过待激活</span>
          <span v-if="scope.row.state == 3" style="color:#10cb10">激活成功</span>
          <span v-if="scope.row.state == 4" style="color:red">驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="店铺地址" width="180">
      </el-table-column>
      <el-table-column prop="logo" label="logo" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.logo" style="wight:100px;height: 100px"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="success" v-if="scope.row.state==1 || scope.row.state==4" size="small" @click="handleVerify(scope.$index, scope.row)">人工审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-- 批量删除和分页 -->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="this.query.pageSize" :total="this.pageInfo.total" style="float:right;">
            </el-pagination>-->

      <!--      layout 属性加上什么，底部就会出现什么-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="this.query.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          style="float:right"
          layout="total,sizes, prev, pager, next,jumper"
          :total="this.pageInfo.total">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="100px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="店铺名称" >
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺地址">
          <el-input v-model="saveForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺logo" prop="logo">
          <template slot-scope="scope">
            <img :src="saveForm.logo" style="wight:100px;height: 100px"/>
          </template>
        </el-form-item>
        <el-form-item label="店铺logo上传">
          <el-upload
              class="upload-demo"
              action="http://localhost:8081/shop/fastDfs"
              :on-success="handleSuccess"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">待审核</el-radio>
            <el-radio class="radio" :label="2">审核通过待激活</el-radio>
            <el-radio class="radio" :label="3">激活成功</el-radio>
            <el-radio class="radio" :label="4">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" >提交</el-button>
      </div>
    </el-dialog>

    <!--    人工审核-->
    <el-dialog title="人工审核" :visible.sync="verifyFormVisible" :close-on-click-modal="false">
      <el-form :model="verifyForm" label-width="90px"  >
        <el-form-item label="审核备注">
          <el-input type="textarea" v-model="verifyForm.content"  auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button   @click.native="verifyFormVisible = false">取消</el-button>
        <el-button type="danger" @click.native="verifyReturn">驳回</el-button>
        <el-button type="success" @click.native="verifyPass">审核通过</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'

export default {
  data() {
    return {
      query: {  // 定义高级查询的 属性，也是要通过请求带给后端的
        keyword: '',
        currentPage:1,
        pageSize:10
      },
      pageInfo:{
        dataList: [],
        total: 0,
      },
      page: 1,
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,//新增界面是否显示
      saveFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },

      //新增界面数据
      saveForm: {
        id: null,
        name: " ",
        address:"",
        logo:"",
        state: " ",
        manager:{
          id:null,
          realName:""
        }
      },

      // 人工审核
      verifyFormVisible: false,
      verifyForm: {
        id: null,
        content:"",
        verifyResult:""
      },

      // 上传图片
      fileList:[{name:"",url:""}]
    }
  },

  methods: {
    // 人工审核
    handleVerify(index,row){
      this.verifyFormVisible=true
      this.verifyForm.id=row.id
    },

    // 审核通过
    verifyPass(){
      this.verifyForm.verifyResult=true
      this.verifyRequest(this.verifyForm)
    },

    // 审核驳回
    verifyReturn(){
      this.verifyForm.verifyResult=false
      this.verifyRequest(this.verifyForm)
    },

    // 审核请求
    verifyRequest(v){
      console.log("请求的form："+v)
      this.$http.put("/shop/verify",v)
          .then(res=>{
            this.verifyFormVisible=false
            this.getShops()
          }).catch(res=>{
            this.$message(res.data.msg)
      })
    },

    // 上传
    handleRemovePhoto(file) {
    },

    handleSuccess(response){
      this.saveForm.logo=response.ajaxObj
      let para = Object.assign({}, this.saveForm);  // JSON 格式的转换
      // 发请求
      this.$http.post("/shop",para)
          .then((result) => {
            result=result.data;
            if(result.success){
              this.$message({message: '保存成功',type: 'success'});
              this.saveFormVisible=false
              // 清除上传后的文件列表
              this.$refs.upload.clearFiles()
              this.getShops()
            }else {
              this.$message({message: '保存失败',type: 'error'});
            }
          })
          .catch(result => {
            this.$message({message: '网络异常，请稍后访问',type: 'danger'});
          })
    },

    //新增的提交按钮，上级店铺的级联框取数组的最后一个
    addSubmit: function () {
      this.$refs.saveForm.validate((valid) => {   // valid 就是规则的返回值，规则通过了才会有返回值
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            // 上传文件
            this.$refs.upload.submit();
            //NProgress.start();
          });
        }
      });
    },

    // 查询管理员数据 --- 店铺经理 的下拉框需要
    getEmployees(){
      this.$http.get("/employee")
          .then((result) => {
            result=result.data;
            if(result.success){
              this.employees =result.ajaxObj;
            }else {
            }
          })
          .catch(result => {
          })
    },
    // 调整每页数据条数
    handleSizeChange(val){
      this.query.pageSize=val;
      this.getShops()
    },
    // 点击查询按钮
    getQueryShops(){
      this.query.currentPage=1;
      this.getShops()
    },

    handleCurrentChange(val) {
      this.query.currentPage=val;
      this.getShops() // getShops 方法中的 axios 请求会自己来这里找
    },

    //获取店铺列表
    getShops() {
      this.listLoading = true;
      // 发请求
      this.$http.put("/shop",this.query)
          .then(result=>{
            result=result.data;
            if(result.success){
              this.listLoading = false;
              this.pageInfo =result.ajaxObj;
            }else {
              this.$message({message: '查询失败',type: 'error'});
            }
          })
          .catch(result=>{

          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { id: row.id };
        this.$http.delete("/shop/"+row.id)
            .then((result) => {
              result=result.data;
              if(result.success){
                this.$message({message: '删除成功',type: 'success'});
                this.listLoading = false;
                this.query.currentPage=1;
                this.getShops()
              }else {
                this.$message({message: '删除失败',type: 'error'});
              }
            }).catch();
      }).catch(() => {

      });
    },
    //显示编辑界面的弹窗 --- 下拉框的回显和级联框的回显(掐头去尾，头是空字符串，尾是自己)
    handleEdit: function (index, row) {
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);   // 回显
      this.fileList[0].url=this.saveForm.logo
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/shop",ids)
            .then((result) => {
              result=result.data;
              if(result.success){
                this.$message({message: '批量删除成功',type: 'success'});
                this.listLoading = false;
                this.query.currentPage=1;
                this.getShops()
              }else {
                this.$message({message: '批量删除失败',type: 'error'});
              }
            })
            .catch(result => {

            })
      }).catch(()=>{})
    }
  },


  mounted() {
    this.getShops();
  }
}

</script>

<style scoped>

</style>